package com.sise.model;

import java.io.Serializable;

/**
 * @author idea
 * @data 2019/5/27
 */
public class StringItem implements Serializable, CharSequence, Comparable<StringItem> {


    private static final long serialVersionUID = 18413542198431L;


    private char[] items;

    private int length;

    private int offset;

    private int hash = 0;

    public StringItem(String str) {
        this.items = str.toCharArray();
        this.length = items.length;
        this.offset = 0;
    }

    public StringItem(char[] items, int length, int offset, int hash) {
        this.items = items;
        this.length = length;
        this.offset = offset;
        this.hash = hash;
    }

    @Override
    public int length() {
        return length;
    }

    /**
     * 获取词组
     *
     * @return
     */
    public String getWord() {
        return String.valueOf(items);
    }

    @Override
    public char charAt(int index) {
        return items[index];
    }

    @Override
    public CharSequence subSequence(int start, int end) {
        return null;
    }

    @Override
    public int compareTo(StringItem o) {
        return 0;
    }

    /**
     * 计算整个字符串的hash
     *
     * @param key
     * @return
     */
    public static int countHash(String key) {
        int h = 0;
        return (key != null) ? h = (key.hashCode()) ^ (h >>> 16) : 0;
    }

    /**
     * 计算头两个字符的hash值
     *
     * @return
     */
    public int countTwoCharHash(int i) {
        int h = 0;
        if (items.length >= 2) {
            String prefix = String.valueOf(items[i] + items[i + 1]);
            return (prefix != null) ? h = (prefix.hashCode()) ^ (h >>> 16) : 0;
        }
        return h;
    }
}
